package com.example.demo.excel;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.example.demo.mode.User;
import org.junit.Before;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author pengyifeng
 * @version 2.0
 * @ClassName TestExport.java
 * @Description TODO
 * @createTime 2021年11月03日 15:13:00
 */
public class TestExport {
    List<User> userList;
    @Before
    public void testData(){
         userList=new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setName("小明"+i);
            user.setAge(16+i);
            userList.add(user);
        }


    }
    @Test
    public void testMap() throws FileNotFoundException {
        ExcelWriter writer = ExcelUtil.getBigWriter();
        // 重命名第一个Sheet的名称，不然会默认多出一个Sheet1的页
        writer.renameSheet(0, "项目sheet1");
        for (int i = 0; i < 10; i++) {
            // 指定要写出的 Sheet 页
            writer.setSheet("项目sheet"+i);
            //自定义标题别名
            writer.addHeaderAlias("name", "1姓名");
            writer.addHeaderAlias("age", "1年龄");
            writer.write(userList, true);
            writer.addHeaderAlias("name", "姓名》》》》》》》");
            writer.addHeaderAlias("age", "年龄》》》》》》》》");
            writer.write(userList, true);

        }


        FileOutputStream outputStream = new FileOutputStream("D:/home/excel/testHutoolExport.xlsx");
        writer.flush(outputStream, true);
        writer.close();
    }
}
